package com.algorithm.ch2.cjm.dynamic;

/**
 * Created by jieming.chen on 2020/7/5
 */
public class CanJump {

    public static void main(String[] args) {
        CanJump canJump = new CanJump();
        int[] nums = {3,2,1,0,4};
        boolean b = canJump.canJump(nums);
        System.out.println(b);
    }

    public boolean canJump(int[] nums) {
        if (nums.length == 1) {
            return true;
        }
        int fn = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                if (fn <= i) {
                    return false;
                }
            }
            fn = Math.max(fn, nums[i] + i);
            if (fn >= nums.length-1) {
                return true;
            }
        }
        return true;
    }
}
